﻿#region File Header

/* File Name    : FacebookPostManager.cs
 * Description  : Manage Facebook Posts
 * Log
 * Date             Author              Description
 * 18-Oct-2011      Robin Jome          Created the class
 * 19-Oct-2011      Robin Jome          Implemented GetUrl() method to create the Url
 *                                      LikeThePost(),UnLikeThePost() methods added
 */

#endregion

#region Namespaces

using FacebookAPI.Base;
using FacebookAPI.FacebookProperties;
using FacebookAPI.Objects;

#endregion

namespace FacebookAPI.Managers
{
    #region Class

    /// <summary>
    /// Contains methods to handle Facebook Post
    /// </summary>
    public class FacebookPostManager
    {
        #region Methods

        /// <summary>
        /// Creates a Facebook post
        /// </summary>
        /// <param name="post">The post object</param>
        /// <param name="accessToken">The Facebook access token</param>
        /// <returns>Returns the Id of the newly created the post</returns>
        public string CreatePost(Post post, string accessToken)
        {
            FacebookManager mgr = new FacebookManager();
            FacebookBaseObject response = mgr.SaveObject<FacebookBaseObject>(mgr.GetUrl("feed", null, null, null, accessToken, false), post.GetPostData());
            return (response != null) ? response.Id : null;
        }

        /// <summary>
        /// Gets a Post by its Id
        /// </summary>
        /// <param name="postId">The Id of the Facebook Post</param>
        /// <param name="accessToken">The Facebook access token</param>
        /// <returns>Returns a Facebook post object</returns>
        public Post GetPostById(string postId, string accessToken)
        {
            FacebookManager mgr = new FacebookManager();
            return mgr.GetObject<Post>(mgr.GetUrl(postId, null, null, null, accessToken, false));
        }

        /// <summary>
        /// Likes a post
        /// </summary>
        /// <param name="postId">The Id of the Facebook post</param>
        /// <param name="accessToken">The Facebook access token</param>
        /// <returns>Returns true if liked the post</returns>
        public bool LikeThePost(string postId, string accessToken)
        {
            FacebookManager mgr = new FacebookManager();
            return mgr.SaveObject<bool>(mgr.GetUrl(postId, FacebookConnections.Likes, accessToken));
        }

        /// <summary>
        /// Un-likes a post
        /// </summary>
        /// <param name="postId">The Id of the Facebook post</param>
        /// <param name="accessToken">The Facebook access token</param>
        /// <returns>Returns true if unlike was successful</returns>
        public bool UnlikeThePost(string postId, string accessToken)
        {
            FacebookManager mgr = new FacebookManager();
            return mgr.DeleteObject<bool>(mgr.GetUrl(postId, FacebookConnections.Likes, accessToken));
        }

        #endregion

        #region Connections

        /// <summary>
        /// Gets the comments posted for a Post
        /// </summary>
        /// <param name="postId">The Id of the Facebook post</param>
        /// <param name="accessToken">The Facebook access token</param>
        /// <returns>Returns a list of Comments posted for a Facebook Post</returns>
        public FacebookConnection<Comment> GetComments(string postId, string accessToken)
        {
            FacebookManager mgr = new FacebookManager();
            return mgr.GetConnectionObject<Comment>(mgr.GetUrl(postId, FacebookConnections.Comments, null, null, accessToken, false));
        }

        /// <summary>
        /// Gets the list of users who liked a post
        /// </summary>
        /// <param name="postId">The Id of the Facebook Post</param>
        /// <param name="accessToken">The Facebook access token</param>
        /// <param name="searchOptions">The search options to be used</param>
        /// <returns>Returns a list of Users(Id,Name) who liked the Facebook post</returns>
        public FacebookConnection<FacebookBaseObject> GetLikes(string postId, SearchOptions searchOptions, string accessToken)
        {
            FacebookManager mgr = new FacebookManager();
            return mgr.GetConnectionObject<FacebookBaseObject>(mgr.GetUrl(postId, FacebookConnections.Likes, null, searchOptions, accessToken, false));
        }

        #endregion
    }

    #endregion
}
